package com.example.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dao.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 用户关联mapper
 */
@Repository
public interface UserMapper extends BaseMapper<User> {
    /**
     * 根据相关条件分页查询
     * @param user
     * @return
     */
    @Select("<script>"+
            "select " +
            "    id,name,username,email,mobile,account_non_expired as accountNonExpired," +
            "    account_non_locked as account_non_locked," +
            "    credentials_non_expired as credentialsNonExpired," +
            "    enabled " +
            "from " +
            "sys_users" +
            "<trim prefix=\"where\" prefixOverrides=\"and\">" +
            "            <if test=\"name!=null and name!=''\">" +
            "                and name like concat('%',#{name},'%') " +
            "            </if>" +
            "            <if test=\"mobile!=null and mobile!=''\">" +
            "                and mobile like concat(#{mobile},'%') " +
            "            </if>" +
            "</trim>" +
            "LIMIT #{startIndex},#{pageSize};" +
            "</script>")
    List<User> findUsersByPage(User user) ;

    /**
     * 根据相关条件查询总数
     * @param user
     * @return
     */
    @Select("<script>"+
            "select count(1) total " +
            "from " +
            "  sys_users" +
            "<trim prefix=\"where\" prefixOverrides=\"and\">" +
            "            <if test=\"name!=null and name!=''\">" +
            "                and name like concat('%',#{name},'%')" +
            "            </if>" +
            "            <if test=\"mobile!=null and mobile!=''\">" +
            "                and mobile like concat(#{mobile},'%')" +
            "            </if>" +
            "</trim>" +
            "</script>")
    int findCount(User user);

    /**
     * 根据用户账号查询总数，检查用户账号是否重复
     * @param param
     * @return
     */
    @Select("<script>"+
            "select count(1) total " +
            "from " +
            "  sys_users" +
            "<trim prefix=\"where\" prefixOverrides=\"and\">" +
            " username=#{username}" +
            " <if test=\"id!=0\">" +
            "   and id!=#{id}" +
            "</if>" +
            "</trim>" +
            "</script>")
    int findCountByUsername(Map<String,Object> param);

    /**
     * 根据email查询总数，检查email是否重复
     * @param param
     * @return
     */
    @Select("<script>"+
            "select count(1) total" +
            " from " +
            "  sys_users" +
            "<trim prefix=\"where\" prefixOverrides=\"and\">" +
            " email=#{email}" +
            " <if test=\"id!=0\">" +
            "   and id!=#{id}" +
            "</if>" +
            "</trim>" +
            "</script>")
    int findCountByEmail(Map<String,Object> param);

    /**
     * 根据手机号查询总数，检查手机号是否重复
     * @param param
     * @return
     */
    @Select("<script>"+
            "select count(1) total" +
            " from " +
            "  sys_users" +
            "<trim prefix=\"where\" prefixOverrides=\"and\">" +
            " mobile=#{mobile}" +
            " <if test=\"id!=0\">" +
            "   and id!=#{id}" +
            "</if>" +
            "</trim>" +
            "</script>")
    int findCountByMobile(Map<String,Object> param);

}
